/**
 * Created by Administrator on 2016/6/27.
 */

var Request = new Object();
Request = getRequest();
var arrDate = Request['arrDate'];
var depDate = Request['depDate'];
var number  = Request['number'];
var priceCode = Request['priceCode'];
var roomCode = Request['roomCode'];
console.log(Request);
$(function () {

    if(Request['arrDate']){
        $("#arrDate").val(Request['arrDate']);
    }
    if(Request['roomName']){
        $("#roomName").html(decodeURI(Request['roomName']));
    }
    if(Request['depDate']){
        $("#depDate").val(Request['depDate']);
    }

    if(Request['number']){
        $("#guestNums").val(Request['number']);
    }
    if(Request['priceCode']){
        $("#priceCode").attr("value",Request['priceCode']);

    }
    if(Request['priceDetail']){
        $("#priceDetail").attr("value",Request['priceDetail']);

    }
    if(Request['number']){
        $("#roomNums").attr("value",Request['number']);

    }

    if(Request['roomCode']){
        $("#roomCode").attr("value",Request['roomCode'])
    }
    if(Request['priceDetail']){
        $("#hoomPrice").html("value","￥"+Request['priceDetail'])
    }
    if(Request['priceDetail']){
        $("#hoomPrice1").html("value","￥"+Request['priceDetail'])
    }

    //   var userinfo = JSON.parse(appStorage.getItem('usertoken'));

//   console.log(userinfo);
//    $("#priceCode")
    queryPriceInfo(roomCode,priceCode);
    //获取页面参数
    //alert(window.location.href);
    //alert(GetArgsFromHref(window.location.href,"id"));
    //  var id=GetArgsFromHref(window.location.href,"id");
    /*
     $.getJSON(server+"/hotelRoom/get/"+id,
     function(json){
     console.log(json.bizData.bizData);
     var hotemRoomVO=json.bizData.bizData;
     $("#roomName").text(hotemRoomVO.name);
     $("#hoomPrice").text("￥"+hotemRoomVO.roomPrice);
     $("#hoomPrice2").text("￥"+hotemRoomVO.roomPrice);
     $("#orderPrice").val(hotemRoomVO.roomPrice);
     $("#hoomDescription").html("<p>"+hotemRoomVO.description+"</p>");
     });
     */
    //todo


    $("#btnSumbit").click(function () {

        if(!checkData()){
            return false;
        }
        var userinfo = JSON.parse(appStorage.getItem('usertoken'));

        var formDataArray=$("#frmHomeOrder").serializeArray();
        formDataArray.push({"name":"roomName","value":$("#roomName").html()});
        if(appStorage.getItem("usertoken")==undefined){
            formDataArray.push({"name":"memtyp","value":"0"});
        }else{
            $("#div_noLogin").hide();
            //debugger;
            var userinfo = JSON.parse(appStorage.getItem('usertoken'));
            //   console.log(userinfo);
            //   console.log($("#div_okLogin span"));
            //      $("#div_okLogin span").get(0).innerText=userinfo.name;
            //   $("#div_okLogin span").get(1).innerText=userinfo.desc;
            formDataArray.push({"name":"memtyp","value":"3"});
            formDataArray.push({"name":"icnum","value":userinfo.icnum});
            formDataArray.push({"name":"icpwd","value":userinfo.pwd});
        }

        $.ajax({
            async:false,
            url: server+"/smit/makeOrder",
            type: "POST",
            dataType:'json',
            data: formDataArray,
            //        timeout: 5000,
            beforeSend: function(){
                //jsonp 方式此方法不被触发.原因可能是dataType如果指定为jsonp的话,就已经不是ajax事件了
            },
            success: function (json) {//客户端jquery预先定义好的callback函数,成功获取跨域服务器上的json数据后,会动态执行这个callback函数
                if(json.bizData.bizData.code != 1){
                    weui.alert("订单生成出错,请检查");
                }else{
                    //   weui.alert("订单生成成功!订单编号"+json.bizData.bizData.data.orderNo)
                    alert("订单生成成功!订单编号"+json.bizData.bizData.data.orderNo)
                    window.location.href="success.html";
                }

            },
            complete: function(XMLHttpRequest, textStatus){

            },
            error: function(xhr){
                //jsonp 方式此方法不被触发.原因可能是dataType如果指定为jsonp的话,就已经不是ajax事件了
                //请求出错处理
                weui.alert("请求出错(请检查相关度网络状况.)");
            }
        });
    });
    getRandomCode();

});
function simpleList() {
    $.getJSON("http://192.168.0.102:8080/hotelRoom/simpleList/1",
        function(json){
            alert(json);
        });
}

function makeOrder() {
    $.getJSON("http://192.168.0.102:8080/hotelRoom/simpleList/1",
        function(json){
            alert(json);
        });
}
function getRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
/*函数功能：请求远程获取随机数的图片
 *sHref:   http://www.artfh.com/arg.htm?arg1=d&arg2=re
 *sArgName:arg1, arg2
 *return:    the value of arg. d, re
 */
function getRandomCode(){
    $.getJSON(server+"/hotelRoom/orderRandCode",
        function(json){
            $(".yzm img").attr("src",json.bizData.randcode)
        });
}
/*
 *函数功能：从href获得参数
 *sHref:   http://www.artfh.com/arg.htm?arg1=d&arg2=re
 *sArgName:arg1, arg2
 *return:    the value of arg. d, re
 */
function GetArgsFromHref(sHref, sArgName)
{
    var args    = sHref.split("?");
    var retval = "";

    if(args[0] == sHref) /*参数为空*/
    {
        return retval; /*无需做任何处理*/
    }
    var str = args[1];
    args = str.split("&");
    for(var i = 0; i < args.length; i ++)
    {
        str = args[i];
        var arg = str.split("=");
        if(arg.length <= 1) continue;
        if(arg[0] == sArgName) retval = arg[1];
    }
    return retval;
}
function queryPriceInfo(roomCode,priceCode){
    var vo={arrDate:$("#arrDate").val(),depDate:$("#depDate").val(),roomCode:roomCode} ;
    $.ajax({
        async:false,
        url: server+"/smit/queryPriceInfo",
        type: "GET",
        data:vo,
        timeout: 5000,
        beforeSend: function(){
            //jsonp 方式此方法不被触发.原因可能是dataType如果指定为jsonp的话,就已经不是ajax事件了
        },
        success: function (json) {//客户端jquery预先定义好的callback函数,成功获取跨域服务器上的json数据后,会动态执行这个callback函数
            var rows=json.bizData.bizData.data;

            for (var i=0;i<rows.length;i++){
                var hotelRoomVO=rows[i];
                if(priceCode==hotelRoomVO.priceCode){
                    $("#priceCode").attr("value",hotelRoomVO.priceCode );
                    $("#roomCode").attr("value",hotelRoomVO.roomCode );
                    $("#priceDetail").attr("value",hotelRoomVO.priceDetail );
                    $("#priceName").attr("value",hotelRoomVO.priceName );
                    $("#breakfast").attr("value",hotelRoomVO.breakfast );
                    //             $("#hoomPrice").text("￥"+hotelRoomVO.priceDetail);
                    //           $("#hoomPrice2").text("￥"+hotelRoomVO.priceDetail);
                    $("#hoomPrice").text("合计:￥"+ getSumPrice(hotelRoomVO.priceDetail));
                    $("#hoomPrice2").text("￥"+ getSumPrice(hotelRoomVO.priceDetail));

                }

            }
            /*  console.log(json);
             if (json.bizData!=null){
             $("#"+roomCode).html("");
             var rows=json.bizData.bizData.data;
             for (var i=0;i<rows.length;i++){
             console.log(rows[i]);
             var hotelRoomVO=rows[i];
             var href ='dfZhitongche.html?priceCode='+hotelRoomVO.priceCode+'rommCode='+roomCode;
             var html =
             "<a href='"+href+"'>"+
             " <li class='aui-list-view-cell aui-img'>"+
             "<price class='aui-pull-right'><i>¥</i>"+hotelRoomVO.priceDetail+"</price>"+
             "<div class='aui-img-body'>"+
             " <span class='title'>"+hotelRoomVO.priceName+"</span><b> </b>"+
             "<p class='aui-ellipsis-1'><span> </span><span> </span><span> </span></p>"+
             "</div>"+
             "</li>"+
             "</a>";
             $("#"+roomCode).append(html);

             }
             }
             */
        },
        complete: function(XMLHttpRequest, textStatus){

        },
        error: function(xhr){
            //jsonp 方式此方法不被触发.原因可能是dataType如果指定为jsonp的话,就已经不是ajax事件了
            //请求出错处理
            alert("请求出错(请检查相关度网络状况.)");
        }
    });
}

function isdatetime(mystring) {
    var reg = /^(\d{4})(-|\/)(\d{1,2})(-|\/)(\d{1,2})$/;
    var str = mystring;
    if (str == "")   {
        return false;
    }
    if (!reg.test(str) || RegExp.$2 <= 12 && RegExp.$3 <= 31) {
        return false;
    }
    return true;
}




function istime(mystring) {
    var reg = /^([01]\d|2[01234]):([0-5]\d|60)$/;
    var str = mystring;
    if (str == "")   {
        return false;
    }
    if (!reg.test(str)) {
        return false;
    }
    return true;
}
function isphone(mystring) {
    var reg = /^[1][358][0-9]{9}$/;
    var str = mystring;
    if (str == "")   {
        return false;
    }
    if (!reg.test(str)) {
        return false;
    }
    return true;
}


function isemail(mystring) {
    var reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    var str = mystring;
    if (str == "")   {
        return false;
    }
    if (!reg.test(str)) {
        return false;
    }
    return true;
}



function isnum(mystring) {
    var reg = /^[0-9]*[1-9][0-9]*$/;
    var str = mystring;
    if (str == "")   {
        return false;
    }
    if (!reg.test(str)) {
        return false;
    }
    return true;
}
function checkData(){
    var flag = true;
    $(".aui-input[name]").each(function(){
        if(!$(this).val()){
            flag =  false;
            weui.alert($(this).attr("placeholder"))
            return false;
        }
    });


    if(!isdatetime($("#arrDate").val())){
        weui.alert($("#arrDate").attr("placeholder"));
        return false;
    }
    if(!isdatetime($("#depDate").val())){
        weui.alert($("#depDate").attr("placeholder"));
        return false;
    }
    if(!istime($("#fast_arr_tm").val())){
        weui.alert($("#fast_arr_tm").attr("placeholder"));
        return false;
    }
    if(!istime($("#last_arr_tm").val())){
        weui.alert($("#last_arr_tm").attr("placeholder"));
        return false;
    }
    if(!isemail($("#guestMail").val())){
        weui.alert($("#guestMail").attr("placeholder"));
        return false;
    }
    if(!isnum($("#guestNums").val())){
        weui.alert($("#guestNums").attr("placeholder"));
        return false;
    }
    if(!isnum($("#roomNums").val())){
        weui.alert($("#roomNums").attr("placeholder"));
        return false;
    }
    if(!isphone($("#guestPhone").val())){
        weui.alert($("#guestPhone").attr("placeholder"));
        return false;
    }

    return flag;
}

function getSumPrice(priceDet){
    var arr = priceDet.split(",");
    var  sum = 0;
    for(var i =0 ; i< arr.length; i++){
        sum += parseFloat(arr[i]);
    }
    return sum ;
}